package jbase.project.system.net;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

import jbase.project.system.core.common.EventHook;
import jbase.project.system.exception.SysException;

public class ConnectChannel {

	private EventHook hook;

	public boolean isAlive;
	public InputStream inputStream;
	public OutputStream outputStream;
	public String host;

	public String ip;
	public int port;

	public long createDate = 0;
	public long lastActive = 0;

	private int timeout;

	public ConnectChannel(EventHook hook, String host, int timeout) throws SysException {
		this.hook = hook;
		this.createDate = System.currentTimeMillis();
		this.lastActive = this.createDate;
		this.host = host;
		this.timeout = timeout;
		try {
			this.ip = this.host.split(":")[0];
			this.port = this.host.split(":").length > 1 ? Integer.parseInt(this.host.split(":")[1]) : 80;
			@SuppressWarnings("resource")
			Socket hostSck = new Socket(this.ip, this.port);
			if (this.timeout > 0) {
				hostSck.setSoTimeout(this.timeout);
			}
			this.inputStream = hostSck.getInputStream();
			this.outputStream = hostSck.getOutputStream();
			this.isAlive = true;
		} catch (IOException e) {
			hook.event("exception", e);
			hook.event("find.exception.host", host);
			close();
			throw new SysException(e);
		}
	}

	public ConnectChannel(EventHook hook, String host) throws SysException {
		this(hook, host, 0);
	}

	@SuppressWarnings("resource")
	public void reinit() throws SysException {
		this.close();
		this.createDate = System.currentTimeMillis();
		this.lastActive = this.createDate;
		try {
			Socket hostSck = new Socket(this.ip, this.port);
			if (this.timeout > 0) {
				hostSck.setSoTimeout(this.timeout);
			}
			this.inputStream = hostSck.getInputStream();
			this.outputStream = hostSck.getOutputStream();
			this.isAlive = true;
		} catch (IOException e) {
			hook.event("exception", e);
			hook.event("find.exception.host", host);
			close();
			throw new SysException(e);
		}
	}

	public ConnectChannel(EventHook hook, String host, InputStream inputStream, OutputStream outputStream,
			boolean isAlive) {
		this.hook = hook;
		this.host = host;
		this.ip = this.host.split(":")[0];
		this.port = this.host.split(":").length > 1 ? Integer.parseInt(this.host.split(":")[1]) : 80;
		this.inputStream = inputStream;
		this.outputStream = outputStream;
		this.isAlive = isAlive;
		this.createDate = System.currentTimeMillis();
		this.lastActive = this.createDate;
	}

	public InputStream getInput() {
		return this.inputStream;
	}

	public OutputStream getOutput() {
		return this.outputStream;
	}

	public boolean writeData(byte[] data) throws SysException {
		try {
			outputStream.write(data);
			outputStream.flush();
			this.lastActive = System.currentTimeMillis();
		} catch (IOException e) {
			throw new SysException(e);
		}
		return true;
	}

	public int readData(byte[] buff) throws SysException {
		try {
			int readCnt = inputStream.read(buff);
			this.lastActive = System.currentTimeMillis();
			return readCnt;
		} catch (IOException e) {
			close();
			throw new SysException(e);
		}
	}

	@Override
	public String toString() {
		return "HOST:" + host + "\r\n" + "ip:" + ip + "\r\n" + "port:" + port + "\r\n" + "isAvlie:" + isAlive;
	}

	public void close() {
		this.isAlive = false;
		try {
			this.inputStream.close();
		} catch (Exception e) {
			this.hook.event("out", "关闭时发生异常！");
		}
		try {
			this.outputStream.close();
		} catch (Exception e) {
			this.hook.event("out", "关闭时发生异常！");
		}
	}

}